package com.zx.wangyixinwen.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.zx.wangyixinwen.MyRequestQueue;
import com.zx.wangyixinwen.R;
import com.zx.wangyixinwen.bean.HeadlinesDetals;
import com.zx.wangyixinwen.bean.News;

import java.util.List;

/**
 * Created by dllo on 16/1/7.
 */
public class HeadlinesListAdapter extends BaseAdapter {
    private Context context;
    private List<HeadlinesDetals> datas;
    private List<News> data;
    private static final int TYPE_ORDINARY = 0;// data
    private static final int TYPE_ONE = 1;// data
    private static final int TYPE_THREE = 2;// data


    public HeadlinesListAdapter(Context context, List<HeadlinesDetals> datas, List<News> data) {
        this.context = context;
        this.datas = datas;
        this.data = data;
//        this.data.get(0).getT1348647909107().remove(0);
    }

//    public void refreshDatas(List<News> data) {
//        this.data = data;
//        notifyDataSetChanged();
//    }

    @Override
    public int getCount() {
        return datas.size() > 0 && datas != null ? datas.size() : 0;
//        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.size() > 0 && datas != null ? datas.get(position) : null;
//        return datas;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if (data.get(0).getT1348647909107().get(position).getImgextra() != null) {
            return 2;
        }
        if (data.get(0).getT1348647909107().get(position).getImgType() == 1) {

            return 1;
        }
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OrViewHolder viewHolder = null;
        OneViewHolder oneViewHolder = null;
        ThreeViewHolder threeViewHolder = null;
        int type = getItemViewType(position);

        if (convertView == null) {
            switch (type) {
                case TYPE_ORDINARY:
                    viewHolder = new OrViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_tv_list_headlines_list, null);
                    viewHolder.title_tv = (TextView) convertView.findViewById(R.id.tv_item_title);
                    viewHolder.body_tv = (TextView) convertView.findViewById(R.id.tv_item_body);
                    viewHolder.imageView = (NetworkImageView) convertView.findViewById(R.id.iv_item_list_img);
                    convertView.setTag(viewHolder);
                    break;
                case TYPE_ONE:
                    oneViewHolder = new OneViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_list_headlines_list_one, null);
                    oneViewHolder.title_big = (TextView) convertView.findViewById(R.id.item_one_list_title);
                    oneViewHolder.body_big = (TextView) convertView.findViewById(R.id.item_one_list_tv);
                    oneViewHolder.imageViewBig = (NetworkImageView) convertView.findViewById(R.id.item_one_list_iv);
                    convertView.setTag(oneViewHolder);
                    break;
                case TYPE_THREE:
//                    if (position!=0) {
                        threeViewHolder = new ThreeViewHolder();
                        convertView = LayoutInflater.from(context).inflate(R.layout.item_three_list_headlines_list, null);
                        threeViewHolder.imageView1 = (NetworkImageView) convertView.findViewById(R.id.item_three_list_one);
                        threeViewHolder.imageView2 = (NetworkImageView) convertView.findViewById(R.id.item_three_list_two);
                        threeViewHolder.imageView3 = (NetworkImageView) convertView.findViewById(R.id.item_three_list_three);
                        threeViewHolder.textView_title = (TextView) convertView.findViewById(R.id.item_three_list_tv);
                        convertView.setTag(threeViewHolder);
//                    }
                    break;
            }
        } else {
            switch (type) {
                case TYPE_ORDINARY:
                    viewHolder = (OrViewHolder) convertView.getTag();
                    break;
                case TYPE_ONE:
                    oneViewHolder = (OneViewHolder) convertView.getTag();
                    break;
                case TYPE_THREE:
                    threeViewHolder = (ThreeViewHolder) convertView.getTag();
                    break;
            }
        }
        switch (type) {
            case TYPE_ORDINARY:
                viewHolder.title_tv.setText(datas.get(position).getTitle());
                viewHolder.body_tv.setText(datas.get(position).getBody());
                viewHolder.imageView.setImageResource(R.mipmap.refresh);
                MyRequestQueue.setNetImage(viewHolder.imageView, datas.get(position).getPicture());
                break;
            case TYPE_ONE:
                oneViewHolder.title_big.setText(datas.get(position).getTitle());
                oneViewHolder.body_big.setText(datas.get(position).getBody());
                MyRequestQueue.setNetImage(oneViewHolder.imageViewBig, datas.get(position).getPicture());
                oneViewHolder.imageViewBig.setImageResource(R.mipmap.refresh);
                break;
            case TYPE_THREE:
                MyRequestQueue.setNetImage(threeViewHolder.imageView1, data.get(0).getT1348647909107().get(position).getImgsrc());
                MyRequestQueue.setNetImage(threeViewHolder.imageView2, data.get(0).getT1348647909107().get(position).getImgextra().get(0).getImgsrc());
                MyRequestQueue.setNetImage(threeViewHolder.imageView3, data.get(0).getT1348647909107().get(position).getImgextra().get(1).getImgsrc());
                threeViewHolder.textView_title.setText(datas.get(position).getTitle());
                break;
        }
        return convertView;
    }


//        if (convertView == null) {
//            convertView = LayoutInflater.from(context).inflate(R.layout.item_tv_list_headlines_list, null);
//            holder.title_tv = (TextView) convertView.findViewById(R.id.tv_item_title);
//            holder.body_tv = (TextView) convertView.findViewById(R.id.tv_item_body);
//            holder.imageView = (NetworkImageView) convertView.findViewById(R.id.iv_item_list_img);
//            MyRequestQueue.setNetImage(holder.imageView, datas.get(position).getPicture());
//            convertView.setTag(holder);
//        } else {
//            holder = (ViewHolder) convertView.getTag();
//        }
//        holder.title_tv.setText(datas.get(position).getTitle());
//        holder.body_tv.setText(datas.get(position).getBody());
////        holder.imageView.setImageResource(R.mipmap.ic_launcher);
//        MyRequestQueue.setNetImage(holder.imageView, datas.get(position).getPicture());
//        notifyDataSetChanged();
//        return convertView;
//    @Override
//    public View getView(int position, View convertView, ViewGroup parent) {
//        ViewHolder holder = new ViewHolder();
//
//        if (convertView == null) {
//            convertView = LayoutInflater.from(context).inflate(R.layout.item_tv_list_headlines_list, null);
//            holder.title_tv = (TextView) convertView.findViewById(R.id.tv_item_title);
//            holder.body_tv = (TextView) convertView.findViewById(R.id.tv_item_body);
//            holder.imageView = (NetworkImageView) convertView.findViewById(R.id.iv_item_list_img);
//            MyRequestQueue.setNetImage(holder.imageView, datas.get(position).getPicture());
//            convertView.setTag(holder);
//        } else {
//            holder = (ViewHolder) convertView.getTag();
//        }
//        holder.title_tv.setText(datas.get(position).getTitle());
//        holder.body_tv.setText(datas.get(position).getBody());
////        holder.imageView.setImageResource(R.mipmap.ic_launcher);
//        MyRequestQueue.setNetImage(holder.imageView, datas.get(position).getPicture());
//        notifyDataSetChanged();
//        return convertView;
//    }

    // 普通的1


    public class OrViewHolder {
        private NetworkImageView imageView;
        private TextView title_tv, body_tv;
    }

    // 三张图的
    public class ThreeViewHolder {
        private NetworkImageView imageView1, imageView2, imageView3;
        private TextView textView_title;
    }

    // 一张图的
    public class OneViewHolder {
        private NetworkImageView imageViewBig;
        private TextView body_big, title_big;
    }


}
